<?php
use CC\Entity\Privilege;

class Backoffice_PrivilegeController extends CC_Controller
{
    /**
     * List and paginate all privileges
     * 
     * URL: /backoffice/privilege
     */
    public function indexAction()
    {
        // DQL query to be paginated and passed to view
        $dql = $this->_em->createQuery('SELECT p FROM CC\Entity\Privilege p JOIN p.flag f ORDER BY f.name ASC, p.name ASC');
        
        $privileges = new Zend_Paginator(new DoctrineExtensions\Paginate\PaginationAdapter($dql));
        $privileges->setCurrentPageNumber($this->_getPage())
                  ->setItemCountPerPage(20)
                  ->setPageRange(10);
        
        $this->view->privileges = $privileges;
    }
    
    /**
     * Create a new privilege
     * 
     * URL: /backoffice/privilege/create
     */
    public function createAction()
    {
        // Create form
        $form = new Backoffice_Model_FormPrivilege(null, $this->_em);
        $request = $this->getRequest();
        
        // Check if request is POST
        if($request->isPost())
        {
            // Check if form is valid
            if($form->isValid($this->_request->getPost()))
            {
                // Create privilege
                $privilege = new Privilege();
                $flag = $this->_em->getRepository('CC\Entity\Flag')->findOneById($request->getParam('flag'));
                
                $privilege->setName($request->getParam('name'));
                $privilege->setDescription($request->getParam('description'));
                $privilege->setFlag($flag);
                
                $this->_em->persist($privilege);
                $this->_em->flush();
                
                // Display message and redirect to index
                $this->_helper->messenger('success', sprintf(Zend_Registry::get('config')->messages->privilege->create, $flag->getName(), $request->getParam('name')));
                return $this->_helper->redirector('index', 'privilege', 'backoffice');
            }
            else
            {
                // Pass errors to view
                $this->view->errors = $form->getErrors();
            }
        }
        
        // Display form
        $this->view->form = $form;
    }
    
    /**
     * Update the privilege passed via the id GET param
     * 
     * URL: /backoffice/privilege/update/id/?
     */
    public function updateAction()
    {
        // Create form model
        $request = $this->getRequest();
        $form = new Backoffice_Model_FormPrivilege(null, $this->_em);
        $form->getElement('submit')->setLabel('Update Privilege');
        $privilege = $this->_em->getRepository('CC\Entity\Privilege')->findOneById($this->_getParam('id'));
        
        // Check for valid privilege
        if($privilege === null)
        {
            // Display error message and redirect to index action
            $this->_helper->messenger('error', Zend_Registry::get('config')->messages->privilege->invalid);
            return $this->_helper->redirector('index', 'privilege', 'backoffice');
        }
        else 
        {
            // Check if request is post
            if($request->isPost())
            {
                // Check if form is valid
                if($form->isValid($this->_request->getPost()))
                {
                    // Update privilege
                    $flag = $this->_em->getRepository('CC\Entity\Flag')->findOneById($request->getParam('flag'));
                    
                    $privilege->setName($request->getParam('name'));
                    $privilege->setDescription($request->getParam('description'));
                    $privilege->setFlag($flag);
                    
                    $this->_em->persist($privilege);
                    $this->_em->flush();
                    
                    // Display message and redirect
                    $this->_helper->messenger('success', sprintf(Zend_Registry::get('config')->messages->privilege->update, $flag->getName(), $request->getParam('name')));
                    return $this->_helper->redirector('index', 'privilege', 'backoffice');
                }
                else
                {
                    // Pass errors to view
                    $this->view->errors = $form->getErrors();
                }
            }
            else 
            {
                // Populate form
                $formData = array(
                    "name" => $privilege->getName(),
                    "description" => $privilege->getDescription(),
                    "flag" => $privilege->getFlag()->getId()
                );
                
                $form->populate($formData);
            }
        }
        
        // Display form
        $this->view->form = $form;
    }
    
    /**
     * Delete the privilege passed by the id GET param.
     * 
     * URL: /backoffice/privilege/delete/id/?
     */
    public function deleteAction()
    {
        $request = $this->getRequest();
        
        // Check if id is set
        if(!isset($request->id))
        {
            // Display error message and redirect to index
            $this->_helper->messenger('error', Zend_Registry::get('config')->messages->privilege->invalid);
            return $this->_helper->redirector('index', 'privilege', 'backoffice');
        }

        $privilege = $this->_em->getRepository('CC\Entity\Privilege')->findOneById($request->id);
        
        // Check if privilege is valid
        if($privilege !== null)
        {
            $flagName = $privilege->getFlag()->getName();
            $privilegeName = $privilege->getName();
            
            // Remove privilege
            $this->_em->remove($privilege);
            $this->_em->flush();
            
            // Display message and redirect to index action
            $this->_helper->messenger('success', sprintf(Zend_Registry::get('config')->messages->privilege->delete, $flagName, $privilegeName));
            return $this->_helper->redirector('index', 'privilege', 'backoffice');
        }
        else
        {
            // Invalid privilege. Display message and redirect to index action
            $this->_helper->messenger('success', Zend_Registry::get('config')->messages->privilege->invalid);
            return $this->_helper->redirector('index', 'privilege', 'backoffice');
        }
    }
}

